<?php

namespace app\callback\controller;

use app\aliyun\controller\AliyunVoice;
use app\zyhsapi\model\ZyhsConsultChatRecordDetails;
use app\zyhsapi\model\ZyhsConsultChatRtcRecord;
use think\Controller;
use think\Log;

/**
 * RTC 录制
 */
class RtcTranscribe extends Controller
{
    private static $LOG_PRE = 'RTC录制的回调';

    public function handle()
    {
        $params = $this->request->param();
//        Log::write(self::$LOG_PRE . '请求数据：params=【' . json_encode($params) . '】', 'log', true);
        if (isset($params['ChannelId']))
        {
            $channelData = explode('_', $params['ChannelId']);
            switch ($channelData[0])
            {
                case 'consult': // 咨询信息
                    $rtcId = (int)($channelData[1] ?? 0);
                    if (!$rtcId)
                    {
                        return;
                    }
                    $zyhsConsultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
                    $rtcInfo = $zyhsConsultChatRtcRecordModel->getInfoById($rtcId);
                    if (!$rtcInfo)
                    {
                        return;
                    }
                    unset($params['TaskId'], $params['ChannelId']);
                    $rtcInfo['extend']['rtc_record_callback'] = $params;
                    //检验录音文件
                    $taskId = AliyunVoice::submitVoice($params['Url']);
                    $data = ['extend' => $rtcInfo['extend']];
                    if ($taskId)
                    {
                        $data['voice_task_id'] = $taskId;
                    }
                    $zyhsConsultChatRtcRecordModel->updateData($rtcId, $rtcInfo['app_type'], $rtcInfo['uid'], $data, 1);
                    break;
                case 'consult2': // 咨询信息
                    $chatDetailId = (int)($channelData[1] ?? 0);
                    $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
                    $zyhsConsultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
                    $chatDetail = $consultChatRecordDetailsModel->getInfoById($chatDetailId);
                    $rtcId = $chatDetail['extend']['rtc_data']['rtc_id'] ?? 0;
                    if ($rtcId)
                    {
                        $rtcInfo = $zyhsConsultChatRtcRecordModel->getInfoById($rtcId);
                        if (!$rtcInfo)
                        {
                            return;
                        }
                        unset($params['TaskId'], $params['ChannelId']);
                        $rtcInfo['extend']['rtc_record_callback'] = $params;
                        //检验录音文件
                        $taskId = AliyunVoice::submitVoice($params['Url']);
                        $data = ['extend' => $rtcInfo['extend']];
                        if ($taskId)
                        {
                            $data['voice_task_id'] = $taskId;
                        }
                        $zyhsConsultChatRtcRecordModel->updateData($rtcId, $rtcInfo['app_type'], $rtcInfo['uid'], $data, 1);
                    }
                default:
            }
        }
    }
}
